<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
import {listYearVaryMaterialOneTo12CO2} from "@/api/carbon-manage/data";

require('echarts/theme/macarons') // echarts theme

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '98%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    material: {
      type: Array,
      required: false
    },
    curYear: {
      type: Number,
      required: 2024
    },
  },
  data() {
    return {
      chart: null,  //创建chartInstance对象
      currentPage: 1,//当前页数*
      totalPage: 0,//总共页数*
      nameArr: [],
      seriesArr: [],

    }
  },
  watch: {
    curYear(newVal, oldVal) {
      let form = {
        year: newVal,
        stage: '4'
      };
      listYearVaryMaterialOneTo12CO2(form).then(res => {
        this.setOptionsVal(res.data.list);
      });
    },
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {//生命周期
    this.$nextTick(() => {
      this.initChart()
    });
    this.nameArr = [];
    this.material.forEach(item => {
      this.nameArr.push(item.name);
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    //初始化echart对象
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      if (!this.chartData.fossiFuelInfo) {
        listYearVaryMaterialOneTo12CO2({year: this.curYear, stage: '4'}).then(res => {
          this.setOptionsVal(res.data.list);
        });
        return;
      }
      this.setOptions();
    },
    getRandomColor() {
      const letters = '0123456789ABCDEF';
      let color = '#';
      for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)]
      }
      return color;
    },
    setOptions() {
      let fossiFuelInfo = this.chartData.fossiFuelInfo;
      let seriesArr = [];
      fossiFuelInfo.forEach(arr => {
        let data = arr.map(item => {
          return item.carbonEmission;
        });
        let color = this.getRandomColor();
        let color2 = this.getRandomColor();
        let series = {
          name: arr[0].name,
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: color,
              lineStyle: {
                color: color,
                width: 2
              },
              areaStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [   //颜色渐变
                  {
                    offset: 0,
                    color: color2
                  },//0%的颜色值
                  {
                    offset: 1,
                    color: color2
                  }//100%的颜色值
                ])
              }
            }
          },
          data: data,
          animationDuration: 2800,
          animationEasing: 'quadraticOut',
          stack: 'all', // 堆叠图的设置*
        };
        seriesArr.push(series);
      });


      let option = {
        title: {
          text: '固碳产品吸收年度统计',
          left: 50
        },
        dataZoom: [ // 控制区域缩放效果的实现*
          {
            type: 'inside', // 缩放的类型  slide代表滑块  inside代表依靠鼠标滚轮
            xAxisIndex: 0
          },
          {
            type: 'slider',
            yAxisIndex: 0,
          }
        ],
        xAxis: {
          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
          boundaryGap: false, //使得一月数据紧挨边缘
          axisTick: {
            show: false
          }
        },
        grid: { //直角坐标系坐标轴的配置
          left: 10,
          right: 37,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {  //工具提示
          trigger: 'axis', //当鼠标移入坐标轴进行工具提示显示
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10],
          formatter: function (params) {
            var relVal = params[0].name
            for (var i = 0, l = params.length; i < l; i++) {
              relVal += '<br/>' + params[i].marker + params[i].seriesName + ' : ' + params[i].value + '(tCO\u2082)'
            }
            return relVal
          }
        },
        toolbox: {
          feature: {
            saveAsImage: {},//导出图片*
            dataView: {},//数据视图*
            restore: {},//重置*
            dataZoom: {},//区域缩放*
            magicType: {
              type: ['line', 'bar']
            }
          }
        },
        yAxis: {
          // name:tCO2,
          axisTick: {
            show: false
          },
          scale: true   //脱离0值比例*
        },
        legend: {
          data: this.nameArr
        },
        series: seriesArr
      }
      this.chart.setOption(option);
    },
    setOptionsVal(val) {
      let fossiFuelInfo = val;
      let seriesArr = [];
      fossiFuelInfo.forEach(arr => {
        let data = arr.map(item => {
          return item.carbonEmission;
        });
        let color = this.getRandomColor();
        let color2 = this.getRandomColor();
        let series = {
          name: arr[0].name,
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: color,
              lineStyle: {
                color: color,
                width: 2
              },
              areaStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [   //颜色渐变
                  {
                    offset: 0,
                    color: color2
                  },//0%的颜色值
                  {
                    offset: 1,
                    color: color2
                  }//100%的颜色值
                ])
              }
            }
          },
          data: data,
          animationDuration: 2800,
          animationEasing: 'quadraticOut',
          stack: 'all', // 堆叠图的设置*
        };
        seriesArr.push(series);
      });


      let option = {
        title: {
          text: '固碳产品吸收年度统计',
          left: 50
        },
        dataZoom: [ // 控制区域缩放效果的实现*
          {
            type: 'inside', // 缩放的类型  slide代表滑块  inside代表依靠鼠标滚轮
            xAxisIndex: 0
          },
          {
            type: 'slider',
            yAxisIndex: 0,
          }
        ],
        xAxis: {
          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
          boundaryGap: false, //使得一月数据紧挨边缘
          axisTick: {
            show: false
          }
        },
        grid: { //直角坐标系坐标轴的配置
          left: 10,
          right: 37,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {  //工具提示
          trigger: 'axis', //当鼠标移入坐标轴进行工具提示显示
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10],
          formatter: function (params) {
            var relVal = params[0].name
            for (var i = 0, l = params.length; i < l; i++) {
              relVal += '<br/>' + params[i].marker + params[i].seriesName + ' : ' + params[i].value + '(tCO\u2082)'
            }
            return relVal
          }
        },
        toolbox: {
          feature: {
            saveAsImage: {},//导出图片*
            dataView: {},//数据视图*
            restore: {},//重置*
            dataZoom: {},//区域缩放*
            magicType: {
              type: ['line', 'bar']
            }
          }
        },
        yAxis: {
          // name:tCO2,
          axisTick: {
            show: false
          },
          scale: true   //脱离0值比例*
        },
        legend: {
          data: this.nameArr
        },
        series: seriesArr
      }
      this.chart.setOption(option);
    }
  }
}
</script>
